package com.ptu.spzx.controller;


import com.github.pagehelper.PageInfo;
import com.ptu.spzx.common.PageResult;
import com.ptu.spzx.domain.Delete.DeleteIds;
import com.ptu.spzx.domain.Message.Message;
import com.ptu.spzx.domain.Message.dto.MessageInsertDTO;
import com.ptu.spzx.domain.Message.dto.MessageQueryDTO;
import com.ptu.spzx.domain.Message.dto.MessageTeacherBatchQueryNoticeDTO;
import com.ptu.spzx.domain.Message.dto.MessageUpdateDTO;
import com.ptu.spzx.domain.Message.vo.StuCheckMessageVO;
import com.ptu.spzx.model.vo.common.Result;
import com.ptu.spzx.model.vo.common.ResultCodeEnum;
import com.ptu.spzx.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author YT
 * @since 2024-01-28
 */
@RestController
@RequestMapping("/sc/exam/notice")
@Slf4j
public class MessageController {
    @Autowired
    IMessageService messageService;

    // 创建通知
    @PostMapping("/insertNotice")
    @PreAuthorize("@permissionService.hasPermission('exam:notice:insertNotice')")
    public Result<Message> add(@RequestBody MessageInsertDTO insertDTO) {
        messageService.add(insertDTO);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // 更新通知
    @PutMapping("/updateNotice")
    @PreAuthorize("@permissionService.hasPermission('exam:notice:updateNotice')")
    public Result<Message> update(@RequestBody MessageUpdateDTO updateDTO) {
        messageService.update(updateDTO);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // 批量删除
    @DeleteMapping("/deleteNotice")
    @PreAuthorize("@permissionService.hasPermission('exam:notice:deleteNotice')")
    public Result<Message> deleteByIds(@RequestBody DeleteIds deleteIds) {
            messageService.deleteByIds(deleteIds);
            return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // 分页查询
    @GetMapping("/queryBatchNotice/{pageNumber}/{pageSize}")
    @PreAuthorize("@permissionService.hasPermission('exam:notice:queryBatchNotice')")
    public Result<PageResult<Message>> batchQuery(
            @PathVariable(value = "pageNumber") Integer pageNumber,
            @PathVariable(value = "pageSize") Integer pageSize,
            @RequestBody MessageQueryDTO queryDTO) {
        PageInfo<Message> pageInfo = messageService.batchQuery(pageNumber, pageSize, queryDTO);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    // 教师的分页查询
    @GetMapping("/queryTeachBatchNotice/{pageNumber}/{pageSize}")
    @PreAuthorize("@permissionService.hasPermission('exam:notice:queryTeachBatchNotice')")
    public Result queryTeachBatchNotice(
            @PathVariable(value = "pageNumber", required = false) Integer pageNumber,
            @PathVariable(value = "pageSize", required = false) Integer pageSize,
            @RequestBody MessageTeacherBatchQueryNoticeDTO queryDTO) {
        PageInfo<Message> pageData = messageService.batchQueryByTeacher(pageNumber, pageSize, queryDTO);
        return Result.build(pageData, ResultCodeEnum.SUCCESS);
    }

    // 学生查看通知
    @GetMapping("/stuIsRead")
    @PreAuthorize("@permissionService.hasPermission('exam:notice:stuIsRead')")
    public Result<StuCheckMessageVO> stuCheckMessage(@RequestParam Long stuId, @RequestParam Long noticeId) {
            StuCheckMessageVO checkVO = messageService.stuChekMessage(stuId, noticeId);
            return Result.build(checkVO, ResultCodeEnum.SUCCESS);
    }

    // 查询指定通知的 已读/总人数
    @GetMapping("/isReadNum")
    @PreAuthorize("@permissionService.hasPermission('exam:notice:isReadNum')")
    public Result<String> getReadNum(Long noticeId) {
            String res = messageService.getReadNum(noticeId);
            return Result.build(res, ResultCodeEnum.SUCCESS);
    }
}

